package com.tmall.testgen.plugin.wizards;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.*;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;

import java.io.*;

import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;

import com.tmall.testgen.client.commond.SendCommondInstance;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.plugin.Activator;
import com.tmall.testgen.plugin.PluginConstant;
import com.tmall.testgen.server.socket.CommondEnum;

/**
 * This is a sample new wizard. Its role is to create a new file 
 * resource in the provided container. If the container resource
 * (a folder or a project) is selected in the workspace 
 * when the wizard is opened, it will accept it as the target
 * container. The wizard creates one file with the extension
 * "mpe". If a sample multi-page editor (also available
 * as a template) is registered for the same extension, it will
 * be able to open it.
 */

public class CreateTestCodeWizad extends Wizard implements INewWizard {
	private CreateTestCodeWizadPage page;
	private ISelection selection;

	/**
	 * Constructor for CreateTestCodeWizad.
	 */
	public CreateTestCodeWizad() {
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle("单元测试生成");
	}
	
	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page = new CreateTestCodeWizadPage(selection);
		addPage(page);
	}
	

	/**
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. We will create an operation and run it
	 * using wizard as execution context.
	 */
	public boolean performFinish() {
		try {
			if(page.finish())
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("com.tmall.testgen.plugin.views.GenCodeMonitorView");
			else
				return false;
		} catch (Exception e) {
			MyMessageDialog.openExceptionDialog(getShell(),  e);
			return false;
		}
		return true;
	}
	
	@Override
	public boolean performCancel() {
		
		return super.performCancel();
	}

	
	/**
	 * We will accept the selection in the workbench to see if
	 * we can initialize from it.
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}